<?php
class score extends Model {
	
	/**
	 * 根据userid获得钱包信息
	 *
	 * @param unknown $userid        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getWalletInfo($userid) {
		$sql = "SELECT SUM(`score_score`) FROM `score` WHERE `user_id` =" . $userid . "";
		return $this->fetchRow ( $sql );
	}
	/**
	 * 根据userid获得积分记录列表
	 */
	public function getScoreRecordList($userid, $timestamp, $pagetype, $num) {
		if ($timestamp == "null" || $timestamp == "" || $timestamp == "NULL") {
			$sql = "SELECT `score`.*,`exercise`.* FROM `score` LEFT JOIN `exercise` ON `score`.`ex_id`=`exercise`.`ex_id`
			 WHERE `score`.`user_id`=$userid AND `score_type`<>4
			 ORDER BY `score_id` DESC LIMIT $num";
		} else {
			if ($pagetype == "pre") {
				$sql = "SELECT `score`.*,`exercise`.* FROM `score` LEFT JOIN `exercise` ON `score`.`ex_id`=`exercise`.`ex_id`
				 WHERE `score`.`user_id`=$userid AND `score_id`>$timestamp AND `score_type`<>4
				 ORDER BY `score_id` DESC LIMIT $num ";
			} else {
				$sql = "SELECT `score`.*,`exercise`.* FROM `score` LEFT JOIN `exercise` ON `score`.`ex_id`=`exercise`.`ex_id`
				 WHERE `score`.`user_id`=$userid AND `score_id`<$timestamp AND `score_type`<>4
				 ORDER BY `score_id` DESC LIMIT $num ";
			}
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 * 根据userid获得积分兑换记录列表
	 */
	public function getExchangeList($userid, $timestamp, $pagetype, $num) {
		// 类别，1-录制审核通过；2-互审成功；3-终审成功；4-兑换现金
		if ($timestamp == "null" || $timestamp == "" || $timestamp == "NULL") {
			$sql = "SELECT `score`.* FROM `score` 
			 WHERE `score`.`user_id`=$userid AND `score`.`score_type`=4
			 ORDER BY `score_id` DESC LIMIT $num";
		} else {
			if ($pagetype == "pre") {
				$sql = "SELECT `score`.* FROM `score`
			 	 WHERE `score`.`user_id`=$userid AND `score`.`score_type`=4 AND `score_id`>$timestamp
				 ORDER BY `score_id` DESC LIMIT $num ";
			} else {
				$sql = "SELECT `score`.* FROM `score`
				 WHERE `score`.`user_id`=$userid AND `score`.`score_type`=4 AND `score_id`<$timestamp
				 ORDER BY `score_id` DESC LIMIT $num ";
			}
		}
		return $this->fetchAll ( $sql );
	}
	public function insertExchangeScore($userid, $point, $money) {
		$sql = "INSERT INTO `score` (`score_type`,`score_score`,`score_time`,`score_money`,`user_id`) 
				VALUES 
				(4, " . - 1 * $point . ",NOW()," . $money . "," . $userid . ")";
		return $this->insert ( $sql );
	}
	/**
	 * 充值
	 *
	 * @param unknown $userid        	
	 * @param unknown $point        	
	 * @param unknown $money        	
	 * @return Ambigous <boolean, number>
	 */
	public function insertMoneyToScore($userid, $point, $money) {
		$sql = "INSERT INTO `score` (`score_type`,`score_score`,`score_time`,`score_money`,`user_id`) 
				VALUES 
				(5, " . $point . ",NOW()," . $money . "," . $userid . ")";
		return $this->insert ( $sql );
	}
	
	/**
	 * 获取积分记录
	 *
	 * @param unknown_type $page        	
	 * @param unknown_type $num        	
	 * @param unknown_type $userid        	
	 * @return multitype:unknown number Ambigous <boolean, multitype:>
	 */
	public function getscorepagemodel($page, $num, $userid) {
		$sql = "SELECT `score`.*, `uuser`.`user_realname` FROM `score`
				LEFT JOIN `uuser` ON `uuser`.`user_id` = `score`.`user_id`
				WHERE `score`.`user_id` = '" . $userid . "'
				ORDER BY `score`.`score_time` DESC";
		$limit = " LIMIT " . ($page - 1) * $num . ", " . $num . "";
		$list = $this->fetchAll ( $sql . $limit );
		$total = count ( $this->fetchAll ( $sql ) );
		$totalPage = ceil ( $total / $num );
		// echo $sql.$limit;
		return array (
				'page' => $page,
				'list' => $list,
				'total' => $total,
				'totalPage' => $totalPage 
		);
	}
	public function modscore($score, $userid) {
		$sql = "INSERT INTO `score` (`score_type`,`score_score`,`score_time`,`score_money`,`user_id`) VALUES (8, " . $score . ", NOW(), " . - $score . "," . $userid . ")";
		return $this->insert ( $sql );
	}
	
	/**
	 * 根据userid和date获得消费记录列表
	 */
	public function getConsumeRecord($userid, $date, $timestamp, $pagetype, $num) {
		// 类别，1-录制审核通过；2-互审成功；3-终审成功；4-兑换现金
		if (! $date)
			$date = "%";
		else
			$date = $date . "%";
		if ($timestamp == "null" || $timestamp == "" || $timestamp == "NULL") {
			$sql = "SELECT `score`.* FROM `score`
			WHERE `score`.`user_id`= $userid AND `score`.`score_score`< 0 AND `score`.`score_time` LIKE '$date'
			ORDER BY `score_id` DESC LIMIT $num";
		} else {
			if ($pagetype == "pre") {
				$sql = "SELECT `score`.* FROM `score`
				WHERE `score`.`user_id`= $userid AND `score`.`score_score`< 0 AND `score`.`score_time` LIKE $date AND `score_id`>$timestamp
				ORDER BY `score_id` DESC LIMIT $num ";
			} else {
				$sql = "SELECT `score`.* FROM `score`
			WHERE `score`.`user_id`= $userid AND `score`.`score_score`< 0 AND `score`.`score_time` LIKE $date AND `score_id`<$timestamp
			ORDER BY `score_id` DESC LIMIT $num ";
			}
		}
		// echo $sql;
		return $this->fetchAll ( $sql );
	}
	/**
	 * 根据userid获得充值记录列表
	 */
	public function getRechargeRecordList($userid, $timestamp, $pagetype, $num) {
		// 类别，1-录制审核通过；2-互审成功；3-终审成功；4-兑换现金
		if ($timestamp == "null" || $timestamp == "" || $timestamp == "NULL") {
			$sql = "SELECT `score`.* FROM `score`
				WHERE `score`.`user_id`= $userid AND `score`.`score_type`=5
				ORDER BY `score_id` DESC LIMIT $num";
		} else {
			if ($pagetype == "pre") {
				$sql = "SELECT `score`.* FROM `score`
					WHERE `score`.`user_id`= $userid AND `score`.`score_type`=5 AND `score_id`>$timestamp
					ORDER BY `score_id` DESC LIMIT $num ";
			} else {
				$sql = "SELECT `score`.* FROM `score`
				WHERE `score`.`user_id`= $userid AND `score`.`score_type`=5  AND `score_id`<$timestamp
				ORDER BY `score_id` DESC LIMIT $num ";
			}
		}
		// echo $sql;
		return $this->fetchAll ( $sql );
	}
	/**
	 * 根据consumeID获得消费详情
	 */
	public function getConsumeDetail($consumeID) {
		$sql = "SELECT * FROM `score` LEFT JOIN `exercise` ON `score`.`ex_id` = `exercise`.`ex_id`
				WHERE `score`.`score_id` =" . $consumeID . "";
		return $this->fetchRow ( $sql );
	}
	
	/**
	 * 根据userid获得问答记录列表
	 */
	public function getANQlist($pageType, $timeStamp, $num, $userid, $beginTime = "null", $endTime = "null", $subID = -1, $knowledgeID = -1, $skillID = -1, $key = "null") {
		$stimestamp = "";
		$sknowsid = "";
		$sskillid = "";
		$stiemSelect = "";
		$ssubId = "";
		if ($timeStamp == "null" || $timeStamp == "" || $timeStamp == "NULL") {
		} else {
			if ($pageType == "pre") {
				$stimestamp = "AND `score_id`>$timeStamp";
			} else {
				$stimestamp = "AND `score_id`<$timeStamp";
			}
		}
		if ($subID != - 1) {
			$asubId = explode ( "_", $subID );
			$ssubId = $ssubId . " AND ( ";
			for($i = 0; $i < count ( $asubId ) - 1; $i ++) {
				$ssubId = $ssubId . " `exercise`.`sub_id`=$asubId[$i] OR";
			}
			$ssubId = $ssubId . " `exercise`.`sub_id`=$asubId[$i]) ";
		}
		if ($knowledgeID != - 1) {
			$aknowsid = explode ( "_", $knowledgeID );
			$sknowsid = $sknowsid . " AND ( ";
			for($i = 0; $i < count ( $aknowsid ) - 1; $i ++) {
				$sknowsid = $sknowsid . " `exercise_knowledge`.`knows_id`=$aknowsid[$i] OR";
			}
			$sknowsid = $sknowsid . " `exercise_knowledge`.`knows_id`=$aknowsid[$i]) ";
		}
		if ($skillID != - 1) {
			$askillid = explode ( "_", $skillID );
			$sskillid = $sskillid . " AND ( ";
			for($i = 0; $i < count ( $askillid ) - 1; $i ++) {
				$sskillid = $sskillid . " `exercise_skill`.`skill_id`=$askillid[$i] OR";
			}
			$sskillid = $sskillid . " `exercise_skill`.`skill_id`=$askillid[$i]) ";
		}
		if ($beginTime && $beginTime != "null") {
			$beginTime = str_replace ( "%20", " ", $beginTime );
			$endTime = str_replace ( "%20", " ", $endTime );
			$stiemSelect = " AND `score`.`score_time` BETWEEN '$beginTime' AND '$endTime' ";
		}
// 		if ($subID != - 1 && $subID != "-1") {
// 			$ssubId = "AND `exercise`.`sub_id`=$subID";
// 		}
		if ($key == "null")
			$key = "";
		
		$key = "%" . $key . "%";
		$sql = "SELECT DISTINCT `score`.score_id,`score`.ex_id,`score`.score_time,`exercise`.ex_title 
			FROM `score` LEFT JOIN `exercise` ON `score`.`ex_id`=`exercise`.`ex_id`
			LEFT JOIN `exercise_knowledge` ON `exercise`.`ex_id`=`exercise_knowledge`.`ex_id`
			LEFT JOIN `knowledge` ON `knowledge`.`knows_id`=`exercise_knowledge`.`knows_id`
			LEFT JOIN `exercise_skill` ON `exercise`.`ex_id`=`exercise_skill`.`ex_id`
			LEFT JOIN `skill` ON `skill`.`skill_id`=`exercise_skill`.`skill_id`
			WHERE `score`.`user_id`= $userid
			" . $stiemSelect . $ssubId . $sknowsid . $sskillid . " 
			AND (`exercise`.`ex_title` LIKE '$key' OR `knowledge`.`knows_name` LIKE '$key' OR `skill`.`skill_name` LIKE '$key')
			AND (`score`.`score_type`=6 OR `score`.`score_type`=7)" . $stimestamp . " ORDER BY `score_id` DESC LIMIT $num";
		// echo $sql;
		return $this->fetchAll ( $sql );
	}
	/**
	 * 判断积分是否足够
	 */
	public function isScoreEnough($aid, $eid = 0, $userid) {
		if ($eid) {
			$sql = "SELECT (SELECT SUM(`score_score`) FROM `score` WHERE `user_id` =$userid)>=
				(SELECT `exercise_type`.`et_score` FROM `exercise`
				LEFT JOIN `exercise_type` ON `exercise_type`.`et_id`= `exercise`.`et_id`
				WHERE `ex_id` = $eid) AS 'result'";
		} else {
			$sql = "SELECT (SELECT SUM(`score_score`) FROM `score` WHERE `user_id` =$userid)>=
				(SELECT `exercise_type`.`et_score` FROM `exercise`
				LEFT JOIN `exercise_type` ON `exercise_type`.`et_id`= `exercise`.`et_id`
				WHERE `ex_id` = (SELECT `answer`.`ex_id` FROM `answer` WHERE `answer`.`answer_id`=$aid)) 
				AS 'result'";
		}
		// echo $sql;
		return $this->fetchRow ( $sql );
	}
	
	/**
	 * 判断是否已经付过积分
	 */
	public function isPaid($userid, $questionID, $answerID) {
		if ($answerID) {
			$sql = "SELECT `score`.`user_id` FROM `score` WHERE `score`.`answer_id`=$answerID AND `score`.`user_id`=$userid";
		} else {
			$sql = "SELECT `score`.`user_id` FROM `score` WHERE `score`.`ex_id`=$questionID AND `score`.`user_id`=$userid";
		}
		// echo $sql;
		return $this->fetchRow ( $sql );
	}
}